package com.roper.yygh.hosp.controller.api;

import com.roper.yygh.common.exception.HospitalException;
import com.roper.yygh.common.helper.HttpRequestHelper;
import com.roper.yygh.common.result.Result;
import com.roper.yygh.common.result.ResultCodeEnum;
import com.roper.yygh.common.utils.MD5;
import com.roper.yygh.hosp.service.DepartmentService;
import com.roper.yygh.hosp.service.HospitalService;
import com.roper.yygh.hosp.service.HospitalSetService;
import com.roper.yygh.hosp.service.ScheduleService;
import com.roper.yygh.model.hosp.Department;
import com.roper.yygh.model.hosp.Hospital;
import com.roper.yygh.model.hosp.Schedule;
import com.roper.yygh.vo.hosp.DepartmentQueryVo;
import com.roper.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Description:
 * Date:      2021/7/10 21:10
 * author:    LPH
 * version:   V1.0
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    // 上传医院的接口
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        // 获取到传递来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramsMap = HttpRequestHelper.switchMap(requestMap);


        // 获取医院传来的签名
        String hospSign = (String) paramsMap.get("sign");
        // 根据传来的医院编码，查询数据库，查询签名
        String hosCode = (String) paramsMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hosCode);
        // 把数据库的key进行MD5
        String signKeyMd5 = MD5.encrypt(signKey);
        // 判断是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR, 500);
        }
        // 调用service的方法
        hospitalService.save(paramsMap);
        return Result.ok();

    }

    // 展示医院的接口
    @PostMapping("/hospital/show")
    public Result showShop(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的医院编号
        String hosCode = (String) map.get("hoscode");
        // 获取医院传来的signKey
        String hospKey = (String) map.get("sign");
        // 获取数据库里当前医院编号的signkey
        String signKey = hospitalSetService.getSignKey(hosCode);
        // 将数据库中的key进行md5加密
        signKey = MD5.encrypt(signKey);
        if(!hospKey.equals(signKey)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR, 500);
        }
        // 调用service的方法进行展示医院的信息
        Hospital hospital = hospitalService.getByHoscode(hosCode);
        return Result.ok(hospital);
    }

    // 添加科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的医院编号
        String hosCode = (String) map.get("hoscode");
        // 获取医院传来的医院key
        String sign = (String) map.get("sign");

        // 根据医院编号获取数据库中的signkey
        String signKey = hospitalSetService.getSignKey(hosCode);
        // 将数据库中的key进行MD5加密
        signKey = MD5.encrypt(signKey);
        if(!sign.equals(signKey)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR, 500);
        }
        // 通过加密key验证，进行保存科室内容
        departmentService.save(map);
        return Result.ok();
    }

    // 查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的医院编号
        String hosCode = (String) map.get("hoscode");
        // 获取医院传来的医院key
        String sign = (String) map.get("sign");
        // 当前页
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        // 每页显示多少条
        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));

        // 根据医院编号获取数据库中的signkey
        String signKey = hospitalSetService.getSignKey(hosCode);
        // 将数据库中的key进行MD5加密
        signKey = MD5.encrypt(signKey);
//        if(!sign.equals(signKey)) {
//            throw new HospitalException(ResultCodeEnum.SIGN_ERROR, 500);
//        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hosCode);
        // 查询科室的内容
        Page<Department> departmentPage = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(departmentPage);
    }

    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {

        // 获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的医院编号
        String hosCode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
        // 获取医院传来的医院key
        String sign = (String) map.get("sign");

        departmentService.removeDepartment(hosCode, depcode);
        return Result.ok();
    }

    // 添加排班接口
    @PostMapping("/schedule/add")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取医院系统传来的消息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的signKey
        String sign = (String) map.get("sign");

        // 调用service的添加接口
        scheduleService.save(map);
        return Result.ok();
    }

    // 查询排班接口
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取医院系统传来的消息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编号
        String hosCode = (String) map.get("hosCode");

        // 获取医院传来的sing (忽略了)
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hosCode);
        // 当前页
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        // 每页显示多少条
        int limit = StringUtils.isEmpty(map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));

        // 查询排班的内容
        Page<Schedule> pageDepartment = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageDepartment);
    }

    // 删除排班接口
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        // 获取医院系统传来的消息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        // 医院编号
        String hoscode = (String) map.get("hoscode");
        // 科室编号
        String hosScheduleId = (String) map.get("hosScheduleId");

        // 获取医院传来的sing
        scheduleService.removeSchedule(hoscode, hosScheduleId);
        return Result.ok();
    }
}
